package org.txazo.music.web.action;

import java.util.Date;

import javax.mail.MessagingException;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.txazo.music.service.UserService;
import org.txazo.music.util.DateUtil;
import org.txazo.music.util.PatternUtil;
import org.txazo.music.util.StringUtil;
import org.txazo.music.commons.annotation.RequireLogin;
import org.txazo.music.commons.mail.Mail;
import org.txazo.music.commons.mail.RegisterConfirmMail;
import org.txazo.music.util.map.RegisterCache;
import org.txazo.music.util.md5.MD5;
import org.txazo.music.vo.User;
import org.txazo.music.web.action.base.BaseAction;

/**
 * 注册
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-12
 * 
 */
@Controller
@Scope("session")
@Namespace("/register")
public class RegisterAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	/** 邮箱帐号 */
	private String account;
	/** 帐号密码 */
	private String password;
	/** 重复密码 */
	private String repeat;
	/** 昵称 */
	private String nickname;
	/** 验证码 */
	private String authcode;

	/** 提示一 */
	private String tip1;
	/** 提示二 */
	private String tip2;
	/** 提示三 */
	private String tip3;
	/** 提示四 */
	private String tip4;
	/** 提示五 */
	private String tip5;

	/** 电子邮件登录URL */
	private String url;

	/** 电子邮件加密地址 */
	private String r;

	@Autowired
	private UserService userService;

	@Autowired
	private JavaMailSender javaMailSender;

	/**
	 * 跳转到注册页面
	 */
	@Action(value = "index", results = { @Result(name = SUCCESS, location = "/register/register.jsp") })
	@RequireLogin
	public String index() {
		request.setAttribute("index", "index");

		return SUCCESS;
	}

	/**
	 * 用户注册
	 */
	@Action(value = "register", results = {
			@Result(name = INPUT, location = "/register/register.jsp"),
			@Result(name = SUCCESS, location = "/register/register-confirm.jsp") })
	public String register() {
		String result = SUCCESS;

		if (StringUtil.isEmpty(account)) {
			tip1 = "请输入常用的邮件地址";
			result = INPUT;
		} else if (!PatternUtil.matchEmail(account)) {
			tip1 = "请输入正确的邮件地址";
			result = INPUT;
		} else if (userService.isAccountExist(account)
				|| RegisterCache.exists(account)) {
			tip1 = "你输入的邮件地址已被注册";
			result = INPUT;
		}

		if (StringUtil.isEmpty(password)) {
			tip2 = "请设置你的帐号登录密码";
			result = INPUT;
		} else if (!PatternUtil.matchNumAndChar(password)) {
			tip2 = "请输入6到18个字符的密码";
			result = INPUT;
		}

		if (StringUtil.isEmpty(repeat)) {
			tip3 = "请再次输入你的登录密码";
			result = INPUT;
		} else if (!repeat.equals(password)) {
			tip3 = "两次输入的密码不一致";
			result = INPUT;
		}

		if (StringUtil.isEmpty(nickname)) {
			tip4 = "请设置你的昵称";
			result = INPUT;
		} else if (!PatternUtil.matchNumAndCharBegin(nickname)) {
			tip4 = "请输入以字母开头的6到18个字符的昵称";
			result = INPUT;
		}

		String validateCode = (String) session
				.getAttribute("registerValidateCode");
		if (StringUtil.isEmpty(authcode)) {
			tip5 = "请输入验证码";
			result = INPUT;
		} else if (!authcode.equals(validateCode)) {
			tip5 = "输入的验证码不正确";
			result = INPUT;
		}

		if (result.equals(INPUT)) {
			return INPUT;
		}

		User user = new User(account, password, nickname, new Date());

		RegisterCache.setUser(MD5.md5OfHex(account).toLowerCase(), user);

		Mail mail = new RegisterConfirmMail();
		try {
			mail.sendMail(javaMailSender, account);
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		url = "http://mail." + account.substring(account.indexOf('@') + 1);

		return result;
	}

	/**
	 * 注册邮箱验证
	 */
	@Action(value = "confirm", results = {
			@Result(name = SUCCESS, location = "/register/register-success.jsp"),
			@Result(name = ERROR, location = "/register/register-error.jsp") })
	public String confirm() {
		User user = RegisterCache.getUser(r);

		if (user == null
				|| DateUtil.differHoursOfDate(user.getRegTime(), new Date()) >= 24) {
			return ERROR;
		}

		RegisterCache.deleteUser(r);

		userService.register(user);

		return SUCCESS;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRepeat() {
		return repeat;
	}

	public void setRepeat(String repeat) {
		this.repeat = repeat;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getAuthcode() {
		return authcode;
	}

	public void setAuthcode(String authcode) {
		this.authcode = authcode;
	}

	public String getTip1() {
		return tip1;
	}

	public void setTip1(String tip1) {
		this.tip1 = tip1;
	}

	public String getTip2() {
		return tip2;
	}

	public void setTip2(String tip2) {
		this.tip2 = tip2;
	}

	public String getTip3() {
		return tip3;
	}

	public void setTip3(String tip3) {
		this.tip3 = tip3;
	}

	public String getTip4() {
		return tip4;
	}

	public void setTip4(String tip4) {
		this.tip4 = tip4;
	}

	public String getTip5() {
		return tip5;
	}

	public void setTip5(String tip5) {
		this.tip5 = tip5;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getR() {
		return r;
	}

	public void setR(String r) {
		this.r = r;
	}

}
